﻿using System;
using System.Windows.Forms;
using Bruce.Net;
using Game.Foundation;

namespace Game.Client
{
    public partial class Form1 : Form
    {
        private MajorConnection _majorClient;

        public string UserName
        {
            get
            {
                return this.txtUserName.Text.Trim();
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        void _majorClient_ReceivedGameList(object sender, GameListEventArgs e)
        {
            this.DoActionCrossThreads(() =>
            {
                treeGameList.Nodes.Clear();

                foreach (var item in e.GameList)
                {
                    TreeNode game = new TreeNode(item.Name);
                    foreach (var area in item.AreaList)
                    {
                        TreeNode areaNode = new TreeNode(area.Name);
                        foreach (var room in area.RoomList)
                        {
                            TreeNode roomNode = new TreeNode(room.Name);
                            string[] paramArray = new string[7];
                            paramArray[0] = item.IPAddress;
                            paramArray[1] = item.Port.ToString();
                            paramArray[2] = area.ID.ToString();
                            paramArray[3] = room.ID.ToString();
                            paramArray[4] = item.Name;
                            paramArray[5] = item.RelativePath;
                            paramArray[6] = item.DownloadPath;

                            roomNode.Tag = paramArray;
                            areaNode.Nodes.Add(roomNode);
                        }

                        game.Nodes.Add(areaNode);
                    }

                    treeGameList.Nodes.Add(game);
                }

            });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AsyncWorkSocket connectionToMajorServer = new AsyncWorkSocket("127.0.0.1", 10086, PacketLoader.Default);
            this._majorClient = new MajorConnection(connectionToMajorServer);
            this._majorClient.ReceivedGameList += new EventHandler<GameListEventArgs>(_majorClient_ReceivedGameList);

            txtUserName.Text = DateTime.Now.Ticks.ToString();
        }

        private void treeGameList_DoubleClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBox.Show("名字不能为空。");
                return;
            }
            if (treeGameList.SelectedNode == null || treeGameList.SelectedNode.Tag == null) return;


            string[] paramArray = treeGameList.SelectedNode.Tag as string[];
            if (paramArray == null) return;

            string key = string.Format("{0} > {1}", paramArray[2], paramArray[3]);
            if (!tabRooms.TabPages.ContainsKey(key))
            {
                if (!System.IO.File.Exists(paramArray[5]))
                {
                    MessageBox.Show(string.Format("游戏尚未安装，请至{0}下载后再试！", paramArray[6]));
                    return;
                }

                AsyncWorkSocket mySocket = new AsyncWorkSocket(paramArray[0], int.Parse(paramArray[1]), PacketLoader.Default);
                RoomControl room = new RoomControl(mySocket, this.UserName, paramArray[4], paramArray[5], int.Parse(paramArray[2]), int.Parse(paramArray[3]));
                room.Text = key;
                tabRooms.TabPages.Add(key, key);
                tabRooms.TabPages[key].Controls.Add(room);
            }

            tabRooms.SelectTab(tabRooms.TabPages[key]);
        }

        private void btnGetGameList_Click(object sender, EventArgs e)
        {
            this._majorClient.SendGameListRequest();
        }
    }
}